package cn.Liujialin.pet.service.impl;


import cn.Liujialin.basic.service.impl.BeanServiceImpl;
import cn.Liujialin.pet.domain.Pet;
import cn.Liujialin.pet.domain.PetDetail;
import cn.Liujialin.pet.mapper.PetDetailMapper;
import cn.Liujialin.pet.mapper.PetMapper;
import cn.Liujialin.pet.service.IPetService;
import cn.Liujialin.user.domain.LoginInfo;
import cn.Liujialin.user.domain.User;
import cn.Liujialin.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BeanServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void save(Pet pet) {
        //保存宠物信息  返回自增ID
        petMapper.save(pet);
        //设置宠物ID并保存宠物相详情
        //不要忘记去重写
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.save(detail);
    }

    @Override
    public void onSale(List<Long> ids) {
        //1.该状态
        //2.设置上架时间
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        petMapper.onsale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        //该状态
        //设置下架时间
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);

    }

    @Override
    public Pet queryDetailById(Long petId) {
        return petMapper.loadByIdDetail(petId);
    }

    @Override
    public void adopt(Map<String, Long> param, LoginInfo currentLoginInfo) {
        Long petId = param.get("petId");
        User user = userMapper.findByLoginInfoId(currentLoginInfo.getId());
        //下架宠物 + 下架时间
        Pet pet = petMapper.findById(petId);
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //绑定领养人
        pet.setUser_id(user.getId());
        petMapper.update(pet);

    }

}
